using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DuplicateFileFinder
{
    public partial class frmDataMatches : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmDataMatches()
        {
            InitializeComponent();
        }
        private System.Collections.Hashtable mGroupList = new System.Collections.Hashtable();

        private Engine mEngine = null;

        public void InitViewer(Engine pEngine)
        {
            mEngine = pEngine;
            UpdateViewer();
        }

        public void UpdateViewer()
        {
  
            if (InvokeRequired)
            {
                this.Invoke(new EventManager.DelegateUpdateViewer(UpdateViewer));
            }
            else
            {
                lstView.Items.Clear();
                lstView.Groups.Clear();
                mGroupList.Clear();
                System.Collections.Hashtable tCountains = new System.Collections.Hashtable();

                foreach (ResultFile tResultFile in mEngine.CompareResultList)
                {
    
                     AddListViewItem(tResultFile);
                     
                }
                lstView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                lstView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                lstView.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                lstView.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                lstView.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
   
        }

        private void AddListViewItem(ResultFile pResultFile)
        {
            if (pResultFile.BinaryMatchList.Count > 0)
            {
                ListViewGroup tGroup = null;
                string tkey = Guid.NewGuid().ToString();
                tGroup = lstView.Groups.Add(tkey, pResultFile.SizeLabel);
                //mGroupList.Add(tkey, tGroup);
                AddItemToGroup(tGroup, pResultFile);

                foreach (ResultFile tResultFile in pResultFile.BinaryMatchList)
                {
                    AddItemToGroup(tGroup, tResultFile);
                }

                
            }
        }

        private void AddItemToGroup(ListViewGroup pGroup, ResultFile pResultFile)
        {
            ListViewItem tItem = lstView.Items.Add(pResultFile.Info.DirectoryName + " - " +  pResultFile.Name);
            tItem.ToolTipText = pResultFile.FileName;

            tItem.Tag = pResultFile;
            tItem.SubItems.Add(pResultFile.SizeLabel);
            tItem.SubItems.Add(pResultFile.Info.CreationTime.ToString());
            tItem.SubItems.Add(pResultFile.Info.LastWriteTime.ToString());
            tItem.SubItems.Add(pResultFile.Info.LastAccessTime.ToString());
            pGroup.Items.Add(tItem);
        }
    }
}